package com.lj.cloudDisk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lj.cloudDisk.config.exception.SpaceException;
import com.lj.cloudDisk.mapper.FileMapper;
import com.lj.cloudDisk.pojo.File;
import com.lj.cloudDisk.service.MediaFileService;
import com.lj.cloudDisk.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(value = "文件上传接口")
@Slf4j
@RestController
@RequestMapping("/media")
public class MediaFileController {

    @Autowired
    MediaFileService mediaFileService;
    @Autowired
    FileMapper fileMapper;

    @ApiOperation("文件上传接口")
    @PostMapping("/upload/{memId}")
    public R uploadFile(MultipartFile file, @RequestParam String catalogue,
                        @PathVariable("memId") String memId) throws IOException {


        log.debug("file:{}",file.toString());
        log.debug("catalogue:{}",catalogue);
        log.debug("memId:{}",memId);

        File newFile = new File();
        //文件大小
        newFile.setSize(file.getSize());
        //原始文件名称
        newFile.setName(file.getOriginalFilename());

        //判断文件是否存在
        boolean isExist = mediaFileService.fileIsExist(memId, catalogue, newFile.getName());
        if(!isExist){
            throw new SpaceException(20001,"文件已存在");
        }
        //创建一个临时文件
        java.io.File tempFile = java.io.File.createTempFile("minio ", ".temp");
        file.transferTo(tempFile);
        //文件路径
        String localFilePath = tempFile.getAbsolutePath();

        //设置文件路径
        newFile.setFDir(catalogue);
        File uploadFile = mediaFileService.uploadFile(newFile,memId,localFilePath);

        return R.success().data("file",uploadFile);
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/deleteFile/{memId}")
    public R deleteFile(@RequestParam("idList") String idList[],@PathVariable String memId){
        log.debug("idList:{},memId:{}", Arrays.toString(idList),memId);

        boolean flag = mediaFileService.removeFile(idList,memId);
        if(!flag){
            return R.success();
        }else {
            return R.error();
        }
    }

    @ApiOperation("音频点播服务")
    @PostMapping("/getPlayAuth/{videoId}")
    public R getPlayAuth(@PathVariable("videoId") String isListVideoId){

        String url = "";
        try {
                //查询文件地址
                LambdaQueryWrapper<File> queryWrapper = new LambdaQueryWrapper();
                queryWrapper.eq(File::getVideoId,isListVideoId);
                File file = fileMapper.selectOne(queryWrapper);
                 url = file.getUrl();

        } catch (Exception e) {
            throw new SpaceException(20001,"获取文件地址失败");
        }
        log.debug("url:{}",url);
        return R.success().data("url",url);
    }
}
